//#preprocessor

package com.grupo83.bbicing.blackberry;

import java.util.Timer; 
import java.util.TimerTask; 
 
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.BitmapField; 
import net.rim.device.api.ui.container.MainScreen;
 
/** 
 * <p>Every application needs a splash screen.</p> 
 * <p>A splash screen should have the following rules</p> 
 * <ul> 
 *     <li>Display effectively what the product is.</li> 
 *  <li>Be removed automatically after a duration if not exited manually.</li> 
 *  <li>You must be able to manually exit the splash screen before the desired amount of time has been exceeded.</li> 
 * </ul> 
 * 
 *     @author Kevin Bayes 
 * 
 *    <p><i>This was modified by the splash screen tutorial found on blackberry.com.</i></p> 
 * 
 */ 
final public class SplashScreen extends MainScreen { 
 
          //private MainScreen next; 
        private Timer timer = new Timer(); 
        private UiApplication application; 
        private SplashInterface _si;
        private BitmapField bp;
        private int splashwidth = 320;
        private int splashheight = 240;
 
        //This splash screen is an image read from the root folder of the application
        private static final Bitmap _bitmap = Bitmap.getBitmapResource("splash/splash.png");
        //Trial version
 
        public SplashScreen( UiApplication ui ) { 
            super ( Field.USE_ALL_HEIGHT | Field.FIELD_LEFT ); 
            this.application = ui;
            _si = (SplashInterface)ui;
            //this.next = next; 
            
            //VerticalFieldManager hfm =  new VerticalFieldManager(  NO_HORIZONTAL_SCROLL | NO_VERTICAL_SCROLL| Field.USE_ALL_HEIGHT | Field.FIELD_HCENTER | Field.FIELD_VCENTER );
            //hfm.add( new BitmapField(_bitmap, FIELD_HCENTER | FIELD_VCENTER ) );

            //this.add(hfm);
            
            bp = new BitmapField(_bitmap );
            splashwidth = Bitmap.getBitmapResource("splash/splash.png").getWidth();
            splashheight = Bitmap.getBitmapResource("splash/splash.png").getHeight();
            
            
            int sideMargin = (Display.getWidth() - splashwidth)/2;
            int topMargin = (Display.getHeight() - splashheight)/2;
            //sideMargin = 20;
            bp.setMargin( topMargin, 0, 0, sideMargin );
            
            this.add( bp ); 
 
            /* 
             * create a listener to allow the user to exit the splash screen 
             * manually or override the navigation events as done below. 
             * 
             * the splash screen listener should implement net.rim.device.api.system.KeyListener. 
             * 
            */ 
            //SplashScreenListener listener = new SplashScreenListener(this); 
            //this.addKeyListener(listener); 
 
            //create a timer to count down to the splash screens exit 
            timer.schedule(new ExitSplashCountDown(), 3000); 
            application.pushScreen (this); 
        } 
 
        /** 
         * <p>When the splash screen exits three things happen</p> 
         * <ol> 
         *     <li>The timer is canceled.</li> 
         * <li>You pop the splash screen off the stack.</li> 
         * <li>You push the main screen onto the stack.</li> 
         * </ol> 
         */        public void dismiss() { 
            timer.cancel(); 
            _si.splashExit();
            application.popScreen (this);
          //  application.pushScreen (next); 
        } 
 
             /** 
         *  <p>This {@link TimerTask} is used to make sure the splash screen exits and 
         *  <b>does not display forever</b>.</p> 
         * 
         * @author Kevin Bayes 
         * 
         */        private final class ExitSplashCountDown extends TimerTask { 
            public void run() { 
               ExitSplashThread dThread = new ExitSplashThread(); 
               application.invokeLater (dThread); 
            } 
        } 
 
              /** 
         * <p>Used by the {@link ExitSplashCountDown} to exit the splash screen.</p> 
         * 
         * @author Kevin Bayes 
         * 
         */        private final class ExitSplashThread implements Runnable { 
            public void run() { 
                 dismiss();
            } 
        }     
 
        /* 
         * When you click the navigation then the screen should exit and go to your applications main screen 
         * 
         * (non-Javadoc) 
         * @see net.rim.device.api.ui.Screen#navigationClick(int, int) 
         */ 
        protected boolean navigationClick(int status, int time) { 
             dismiss(); 
             return true; 
        } 
 
        /* 
         * Make sure nothing happens when the navigation senses movement 
         * 
         * (non-Javadoc) 
         * @see net.rim.device.api.ui.Screen#navigationMovement(int, int, int, int) 
         */ 
        protected boolean navigationMovement(int dx, int dy, int status, int time) { 
            return false; 
        } 
 
        /* 
         * Make sure nothing happens when unclick occurs 
         * 
         * (non-Javadoc) 
         * @see net.rim.device.api.ui.Screen#navigationUnclick(int, int) 
         */ 
        protected boolean navigationUnclick(int status, int time) { 
            return false; 
        } 
        
      //#ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
      /*
      //#endif
        protected boolean touchEvent( TouchEvent message )
        {
            int x = message.getX( 1 );
            int y = message.getY( 1 );
            if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
                // Outside the field
                return false;
            }
            switch( message.getEvent() ) {
           
                case TouchEvent.UNCLICK:
                    dismiss();
                    return true;
            }
            return super.touchEvent( message );
        }
      //#ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
      */
    //#endif 
        
        public interface SplashInterface {
            public void splashExit();
        }
        
    	public void sublayout(int width, int height)
    	{
            int sideMargin = (Display.getWidth() - splashwidth)/2;
            int topMargin = (Display.getHeight() - splashheight)/2;
            //sideMargin = 20;
            bp.setMargin( topMargin, 0, 0, sideMargin );
    	   // Your code here
    	   super.sublayout(width,height);
    	}
}